package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	fileName := "test.txt"
	dir, _ := os.Getwd()
	dirPath := filepath.Join(dir, fileName)
	// O_WRONLY：只写模式。O_CREATE：文件不存在就创建。O_TRUNC：打开并清空文件
	file, err := os.OpenFile(dirPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		return
	}
	n, err := file.WriteString("写入操作")
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	} else {
		fmt.Println("写入字节数（bytes）:", n)
	}
}
